{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 绝对路径\n",
    "\n",
    "![Asked by Facebook](https://img.shields.io/badge/Asked%20by-Facebook-important?style=social&logo=facebook)\n",
    "\n",
    "&emsp;&emsp;给定一个包含目录名、父目录`..`和当前目录`.`的文件路径，返回最短绝对路径（即剔除其中所有的相对路径标记）。\n",
    "\n",
    "```text\n",
    " In[1]: '/Users/Joma/Documents/../Desktop/./../'\n",
    "Out[1]: '/Users/Joma/'\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def shortestPath(path: str) -> str:\n",
    "    if not path:\n",
    "        return None\n",
    "    folders = path.split('/')\n",
    "    result = []\n",
    "    for folder in folders:\n",
    "        if folder == '.':\n",
    "            continue\n",
    "        elif folder == '..':\n",
    "            result.pop()\n",
    "        else:\n",
    "            result.append(folder)\n",
    "    return '/'.join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/Users/Joma/\n"
     ]
    }
   ],
   "source": [
    "print(shortestPath('/Users/Joma/Documents/../Desktop/./../'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
